package com.guet.niit.Agent.dao;

import com.guet.niit.Agent.pojo.Agent;
import com.guet.niit.util.JDBCUtil;
import org.intellij.lang.annotations.Language;

import java.sql.*;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.*;

//任务：实现AgentDao接口里的方法
public class AgentDaoImpl implements AgentDao {
//投资，利润等方法实现
//    数据库的增删改查方法

    @Override
    public List<Agent> InvestFound() {
        /**
         * 投资金额查询
         * 考虑在其中加入代理商的所有信息（id，投资本金，时间，充电站信息）
         */
        Connection con = null;
        try {
            con = JDBCUtil.createConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Statement statement = null;
        ResultSet rs = null;
        Agent invests=null;
        ArrayList<Agent> arr = null;
        try {
//            rs = null;
            PreparedStatement preparedstatement = null;
            String sql = "select * from invest";
            preparedstatement = con.prepareStatement(sql);
            rs = preparedstatement.executeQuery();
            arr=new ArrayList<>();
//            invests = new ArrayList<Agent>();
            while (rs.next()) {
                Long id = rs.getLong("id");
                Long agent_id = rs.getLong("agent_id");
                Long station_id = rs.getLong("station_id");
                int invest_cost = rs.getInt("invest_cost");
                Date update_time = rs.getDate("update_time");
                invests=new Agent(id,agent_id,station_id,invest_cost,update_time);
                arr.add(invests);
            }
        } catch (SQLException e) {
            System.out.println("错误消息" + e.getMessage());
            System.out.println("sql状态" + e.getSQLState());
            System.out.println("错误码" + e.getErrorCode());
        } finally {
            JDBCUtil.closeConnection(con, statement, rs);
        }
        return arr;
    }
//---------------------------------------------------------
    @Override
    public void InvestSelect(Long id) {
        /**
         *
         * 再次让代理商自行输入投资的价格
         * 后面考虑加入限制条件
         * 从键盘获取数据放入数据库中
         * 防止sql注入攻击(未作)
         * 获取充电站信息后选择投资(获取信息放入结果集)
         * （1、可投资的位置查询 2、得到结果后导入充值方法 3、点击选择目标充电站信息进行新增投资记录）
         * 目标：获取信息
         */
//       获取地址信息，用forName导入类名传输返回值
        Scanner sc=new Scanner(System.in);
        int invest_cost=sc.nextInt();
        System.out.println("insert into invest values("+invest_cost+")");
        Connection con= null;
        try {
            con=JDBCUtil.createConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Statement statement=null;
        ResultSet rs=null;
//        Agent agent=new Agent();
//        ArrayList<Agent> arr = null;
//        -------------------------------------
        LocalDateTime now=LocalDateTime.now();
        DateTimeFormatter formatter=DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
        String fomat=formatter.format(now);
//        ============================================
        try {
            statement=con.createStatement();
//            arr=new ArrayList<>();
            String sql = "update invest set invest_cost=" + invest_cost + " where id=" + id;
            int count = statement.executeUpdate(sql);
        } catch (SQLException e) {
            System.out.println("错误消息" + e.getMessage());
            System.out.println("sql状态" + e.getSQLState());
            System.out.println("错误码" + e.getErrorCode());
        } finally {
            JDBCUtil.closeConnection(con, statement, rs);
        }
//        return InvestSelect(id,invest_cost);
    }
//    ---------------------------------------------------------------------------


//--------------------------------------------

    @Override
//    public List<Agent> ProfitFound() {
    public List<Agent> ProfitFound(Long agentId) {
//        点击查询利润表
        Connection con= null;
        try {
            con = JDBCUtil.createConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Statement statement=null;
        ResultSet rs=null;
        Agent profits=null;
        ArrayList<Agent> arr1 = null;
        try {
            PreparedStatement preparedstatement = null;
//            String sql = "select * from profit";
            String sql = "select * from profit where agent_id ="+agentId;
            preparedstatement = con.prepareStatement(sql);
            rs = preparedstatement.executeQuery();
            arr1=new ArrayList<>();
            while (rs.next()){
                Long id = rs.getLong("id");
                Long agent_id = rs.getLong("agent_id");
                Long station_id = rs.getLong("station_id");
                int profit = rs.getInt("profit");
                Date update_time = rs.getDate("update_time");
                profits=new Agent(id,agent_id,station_id,profit,update_time);
                arr1.add(profits);
            }
        } catch (SQLException e) {
            System.out.println("错误消息"+e.getMessage());
            System.out.println("sql状态"+e.getSQLState());
            System.out.println("错误码"+e.getErrorCode());
        }finally {
            JDBCUtil.closeConnection(con,statement,rs);
        }
        return arr1;
    }
//    --------------------------------------------------------

    @Override
    public void ProfitGain(Long id) {
        /**
         * 设置一个公式获取每天的利润以及年总利润展示（随机函数）
         */
        Random random=new Random();
//        设置每日收入的范围（后看情况改）
        int DayStationRevenue=(random.nextInt(30)+10)*100;
        Double DayProf=DayStationRevenue-(100*24*0.5)*0.3;
        Connection con= null;
        try {
            con=JDBCUtil.createConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Statement statement=null;
        ResultSet rs=null;
//        设置时间并本地化
        LocalDateTime now=LocalDateTime.now();
        DateTimeFormatter formatter=DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
        String fomat=formatter.format(now);
//        后期规定利润入库时间为0点
        System.out.println(fomat);
//        ------------------------------------------

        try {
            statement=con.createStatement();
//            在数据库中加入要投资的价格
//            if (rs= statement.executeQuery("select id from table where id = 0"))
            Agent agent=new Agent();
//            String sql = "update profit set profit=?,update_time=? where id=?";
            String sql = "update profit set profit=" + DayProf +" where id=" + id;
//            statement=con.prepareStatement(statement);
//           System.out.println(sql);
            int count = statement.executeUpdate(sql);
        } catch (SQLException e) {
            System.out.println("错误消息"+e.getMessage());
            System.out.println("sql状态"+e.getSQLState());
            System.out.println("错误码"+e.getErrorCode());
        }finally {
            JDBCUtil.closeConnection(con,statement,rs);
        }
    }
}
